from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib
from django.http import JsonResponse

def send(sendadress,theme,content):
    from_addr = ('liuxu0917@163.com')
    password = '371761649'
    smtp_server = 'smtp.163.com'
    msg = MIMEText('{}'.format(content), 'html', 'utf-8')
    msg['From'] = from_addr
    msg['To'] = sendadress
    msg['Subject'] = Header('{}'.format(theme), 'utf-8').encode()
    server = smtplib.SMTP_SSL(smtp_server, 465)
    server.ehlo()
    server.login(from_addr, password)
    server.sendmail(from_addr, [sendadress], msg.as_string())
    server.quit()
def sendmail(request):
    sendadress=request.POST['sendadress']
    theme=request.POST['theme']
    content=request.POST['content']
    send(sendadress,theme,content)
    context={}
    context['sendresult'] = '发送成功'
    return JsonResponse(context)
